package org.kabeja.dxf.helpers;

import org.kabeja.dxf.entity.DXFLine;
import org.kabeja.dxf.entity.DXFPolyline;
import org.kabeja.dxf.entity.DXFVertex;
import org.kabeja.math.MathUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author shutao
 * @since 2022/4/24 17:59
 */
public class DXFUtils {
    public DXFUtils() {
    }

    public static double distance(Point var0, Point var1) {
        return Math.sqrt(Math.pow(var1.getX() - var0.getX(), 2.0D) + Math.pow(var1.getY() - var0.getY(), 2.0D));
    }

    public static double rotateAngleX(Point var0, Point var1) {
        if (var1.getY() == var0.getY()) {
            return 0.0D;
        } else {
            double var2 = var1.getX() - var0.getX();
            double var4 = var1.getY() - var0.getY();
            return Math.toDegrees(Math.atan(1.0D / (var4 / var2)));
        }
    }

    public static double vectorValue(double[] var0) {
        double var1 = 0.0D;

        for (double v : var0) {
            var1 += v * v;
        }

        return Math.sqrt(var1);
    }

    public static Point scalePoint(Point var0, double var1) {
        Point var3 = new Point();
        var3.setX(var0.getX() * var1);
        var3.setY(var0.getY() * var1);
        var3.setZ(var0.getZ() * var1);
        return var3;
    }

    public static Point getPointFromParameterizedLine(Point var0, Vector var1, double var2) {
        Point var4 = scalePoint(var1, var2);
        var4.setX(var4.getX() + var0.getX());
        var4.setY(var4.getY() + var0.getY());
        var4.setZ(var4.getZ() + var0.getZ());
        return var4;
    }

    public static void reverseDXFLine(DXFLine var0) {
        Point var1 = var0.getStartPoint();
        var0.setStartPoint(var0.getEndPoint());
        var0.setEndPoint(var1);
    }

    public static void reverseDXFPolyline(DXFPolyline var0) {
        List<DXFVertex> var1 = new ArrayList<>();
        double var2 = 0.0D;
        int var4 = var0.getVertexCount();

        int var5;
        for(var5 = 0; var5 < var4; ++var5) {
            DXFVertex var6 = var0.getVertex(0);
            double var7 = var6.getBulge();
            if (var7 != 0.0D) {
                var6.setBulge(0.0D);
            }

            if (var2 != 0.0D) {
                var6.setBulge(var2 * -1.0D);
            }

            var2 = var7;
            var1.add(var6);
            var0.removeVertex(0);
        }

        for(var5 = 1; var5 <= var4; ++var5) {
            var0.addVertex(var1.get(var4 - var5));
        }

    }

    public static double getArcRadius(DXFVertex var0, DXFVertex var1) {
        double var2 = 4.0D * Math.atan(Math.abs(var0.getBulge()));
        double var4 = MathUtils.distance(var0.getPoint(), var1.getPoint());
        double var6 = var4 / (2.0D * Math.sin(var2 / 2.0D));
        return var6;
    }

    public static boolean equals(Point var0, Point var1, double var2) {
        return distance(var0, var1) < var2;
    }
}
